package com.itheima.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
//@WebFilter(urlPatterns = "/*")//设置过滤器拦截的请求路径，拦截所有请求
//@WebFilter(urlPatterns = "/login")
//@WebFilter(urlPatterns = "/emps/*") //目录拦截


public class DemoFilter implements Filter {

    /**
     * 初始化方法，在服务器启动时执行一次
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("初始化方法执行了...");
    }
    /**
     * 拦截方法，每次请求被拦截都会执行
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("拦截方法执行了...");
        log.info("放行前逻辑--------------------------");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);

        log.info("放行后逻辑--------------------------");


    }

    /**
     * 销毁方法，在服务器关闭时执行一次
     */

    @Override
    public void destroy() {
        log.info("销毁方法执行了...");
    }
}
